---------------------------------------------------
-- 荣耀峡谷 个人成就/团队成就

-- db.valleyTask.list 		领取记录 [id]=true
-- db.valleyTask.record 	成就进度 [type]=value
---------------------------------------------------

local ValleyExcel = require("excel.valley")
local Util = require("common.Util")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local RoleDBLogic = require("role.RoleDBLogic")
local ValleyLogic = require("valley.ValleyLogic")

local TASK_TYPE_KILLCNT = 1 		-- 杀敌个数
local TASK_TYPE_ROADCNT = 2 		-- 攻破营地个数

-- 状态
local STATE_NONE = 0 				-- 不可领
local STATE_GET = 1 				-- 可领
local STATE_HAD = 2 				-- 已领

-- 封装成就结构体
local function fontTaskNet(net, id, config, human)
	local descConfig = ValleyExcel.taskDesc[config.taskType]
	net.id = id 
	net.desc = Util.format(descConfig.desc, config.taskValue)
	net.state = getTaskState(human, id)
	net.items[0] = #config.items
	for i = 1, net.items[0] do
		local itemID = config.items[i][1]
		local itemCnt = config.items[i][2]
		Grid.makeItem(net.items[i], itemID, itemCnt)
	end
	net.cnt = getValue(human, config.taskType)
	net.maxCnt = config.taskValue
end

-- 查看列表
function sendQuery(human, taskType)
	local msgRet = Msg.gc.GC_VALLEY_TASK_QUERY
	msgRet.taskType = taskType
	msgRet.reds[0] = TASK_TYPE_ROADCNT
	for i = 1, msgRet.reds[0] do
		msgRet.reds[i] = isRedByType(i) and 1 or 0
	end
	local list = getIDsByType(taskType)
	msgRet.list[0] = list and #list or 0
	for i = 1, msgRet.list[0] do
		local id = list[i]
		local config = ValleyExcel.task[id]
		fontTaskNet(msgRet.list[i], id, config, human)
	end
--	Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-- 领取奖励
function getReward(human, id)
	local state = getTaskState(human, id)
	if state == STATE_HAD then
		return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_HADGET)
	end
	if state == STATE_NONE then
		return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_CONDITION)
	end
	local config = ValleyExcel.task[id]
	setGet(human, id)
	for i = 1, #config.items do
		local itemID = config.items[i][1]
		local itemCnt = config.items[i][2]
		BagLogic.addItem(human, itemID, itemCnt, "valley")
	end	

	local msgRet = Msg.gc.GC_VALLEY_TASK_GET
	msgRet.id = id 
	msgRet.reds[0] = TASK_TYPE_ROADCNT
	for i = 1, msgRet.reds[0] do
		msgRet.reds[i] = isRedByType(i) and 1 or 0
	end
	Msg.send(msgRet, human.fd)

	if not isRed(human) then -- 刷新主界面红点
		ValleyLogic.query(human)
	end
end

-- 是否有红点
function isRed(human)
	for taskType in pairs(ValleyExcel.taskDesc) do
		if isRedByType(human, taskType) then
			return true
		end
	end
end

-- 是否有红点，根据任务类型
function isRedByType(human, taskType)
	local list = getIDsByType(taskType)
	if not list then return end

	local value = getValue(human, taskType)
	for _, id in ipairs(list) do
		local state = getTaskState(human, id)
		if state == STATE_NONE then
			break
		end
		if state == STATE_GET then
			return true
		end
	end
end

-- 获取任务状态
function getTaskState(human, id)
	local config = ValleyExcel.task[id]
	if not config then
		return STATE_NONE
	end
	if getValue(human, config.taskType) < config.taskValue then
		return STATE_NONE
	end
	if isGet(human, id) then
		return STATE_HAD
	end
	return STATE_GET
end

local TASKTYPE_2_IDS = nil
function getIDsByType(taskType)
	if not TASKTYPE_2_IDS then
		TASKTYPE_2_IDS = {}
		for id, config in Util.pairsByKeys(ValleyExcel.task) do
			if not TASKTYPE_2_IDS[config.taskType] then
				TASKTYPE_2_IDS[config.taskType] = {}
			end
			local len = #TASKTYPE_2_IDS[config.taskType]
			TASKTYPE_2_IDS[config.taskType][len + 1] = id
		end
	end
	return TASKTYPE_2_IDS[taskType]
end

-- 是否已领取
function isGet(human, id)
	if not human.db.valleyTask then
		return 
	end
	if not human.db.valleyTask.list then
		return 
	end
	return human.db.valleyTask.list[id]
end

-- 设置已领取
function setGet(human, id)
	if not human.db.valleyTask then
		human.db.valleyTask = {} 
	end
	if not human.db.valleyTask.list then
		human.db.valleyTask.list = {} 
	end
	human.db.valleyTask.list[id] = true
end

-- 获取进度值
function getValue(human, taskType)
	if not human.db.valleyTask then
		return 0
	end
	if not human.db.valleyTask.record then
		return 0
	end
	return human.db.valleyTask.record[taskType] or 0
end

-- 刷新任务进度
function updateValue(uuid, killCnt, winCnt)
	if killCnt < 1 and winCnt < 1 then
		return
	end

	local db = RoleDBLogic.getDb(uuid, "valleyTask")
	if not db then return end

	db.valleyTask = db.valleyTask or {}
	db.valleyTask.record = db.valleyTask.record or {}
	local oldKillCnt = db.valleyTask.record[TASK_TYPE_KILLCNT] or 0
	db.valleyTask.record[TASK_TYPE_KILLCNT] = oldKillCnt + killCnt
	local oldWinCnt = db.valleyTask.record[TASK_TYPE_ROADCNT] or 0
	db.valleyTask.record[TASK_TYPE_ROADCNT] = oldWinCnt + winCnt
	RoleDBLogic.saveRoleSset(db)
end